package com.qf.blog.controller;


import com.alibaba.fastjson.JSON;
import com.qf.blog.core.constants.BaseConstants;
import com.qf.blog.core.servlet.BaseServlet;
import com.qf.blog.entity.SystemUser;
import com.qf.blog.service.IUserService;
import com.qf.blog.service.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/UserServlet/*")
public class UserServlet extends BaseServlet {

    private IUserService  userService = new UserServiceImpl();
    //查询数据,
    public void list() throws Exception {
        //查询数据
        List<SystemUser>  list= userService.list();

        Map<String, Object> map = new HashMap<>();
        map.put("UserList",list);
        outView("system/User/UserList",map);

    }


    public String update() throws Exception {

        String json = super.req.getReader().readLine();
        SystemUser systemUser = JSON.parseObject(json, SystemUser.class);
        boolean update = userService.update(systemUser);
        return outJson(update);

    }
    public String delete() throws Exception {
        String id = super.req.getParameter("id");
        boolean remove=userService.remove(Integer.parseInt(id));
        return BaseConstants.BASE_REDIRECT+":"+"UserServlet/list";

    }
    public String add() throws Exception {
        String json = super.req.getReader().readLine();
        SystemUser systemUser = JSON.parseObject(json, SystemUser.class);
        boolean save = userService.save(systemUser);
        return outJson(save);
    }
    public void userById() throws Exception {
        //从请求域里面获取id
        String id = this.req.getParameter("id");
        //业务层调用id,获取到的值
        SystemUser systemUser = userService.byId(Integer.parseInt(id));
        //创建一个map集合
        Map<String,Object> map=new HashMap<>();
        //将后端拿到的id放进集合里面
        map.put("systemUser",systemUser);
        //用封装的outView方法里面的thymeleaf渲染
        outView("/system/User/UserUpdate",map);


    }


}
